package com.cnhis.cloudhealth.clidoctorweb.patientsearch;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.ibatis.annotations.Case;
import org.jboss.resteasy.logging.Logger;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cnhis.cloudhealth.clinical.clidoctor.patientsearch.service.IPatientSearch;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;

import java.util.HashMap;

@Path("patientsearch")
@Component
public class PatientSearchRest
{
    Logger log = Logger.getLogger(PatientSearchRest.class);
    
    @Reference
    private IPatientSearch patientSearch;
    
    /**
     * 588 用于按条件查询门诊病人的 查询条件
     * 
     * @return
     * @throws Exception
     */
    
    @POST
    @Path("searchcondition")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo searchCondition()
        throws Exception
    {
        return patientSearch.queryPatientSickCondition();
    }
    
    /* ScriptId(645) */
    /* Description(医师工作站中检索候诊或转诊病人基本信息) */
    /**
     *
     * @param lVAC51 转诊状态 0 否 1 是
     * @param lPara
     * @param lBCK01 执行科室ID
     * @param lBCE01 医师ID
     * @param BAS02 诊室
     * @param aDate 有效日期
     * @param currPage
     * @param pageSize
     * @param searchcondition 搜素条件
     * @param searchvalue 搜素关键字
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @GET
    @Path("querywaitingandtransfertreatment")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo queryWaitingAndTransferTreatment(@QueryParam("searchcondition") String searchCondition,
        @QueryParam("searchvalue") String searchvalue, @QueryParam("lVAC51") Integer lVAC51,
        @QueryParam("aday") Integer aDay, @QueryParam("lPara") String lPara, @QueryParam("lBCK01") Integer lBCK01,
        @QueryParam("lBCE01") Integer lBCE01, @QueryParam("BAS02") String BAS02, @QueryParam("aDate") String aDate,
        @QueryParam("currPage") int currPage, @QueryParam("pageSize") int pageSize)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("lVAC51", lVAC51);
        hs.put("lPara", lPara);
        hs.put("lBCK01", lBCK01);
        hs.put("lBCE01", lBCE01);
        hs.put("BAS02", BAS02);
        hs.put("aDate", aDate);
        hs.put("searchcondition", searchCondition);
        hs.put("searchvalue", searchvalue);
        return patientSearch.queryWaitingAndTransferTreatment(hs, currPage, pageSize);
        
    }
    
    /* ScriptId(645) */
    /* Description(医师工作站中检索候诊或转诊病人基本信息) */
    /**
     *
     * @param lVAC51 转诊状态 0 否 1 是
     * @param lPara
     * @param lBCK01 执行科室ID
     * @param lBCE01 医师ID
     * @param BAS02 诊室
     * @param aDate 有效日期
     * @param currPage
     * @param pageSize
     * @param searchcondition 搜素条件
     * @param searchvalue 搜素关键字
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @GET
    @Path("querywaitingandtransfertreatment_one")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo querywaitingandtransfertreatment_one(@QueryParam("searchcondition") String searchCondition,
        @QueryParam("searchvalue") String searchValue, @QueryParam("lVAC51") Integer lVAC51,@QueryParam("fenzhen") int fenzhen,
        @QueryParam("aday") Integer aDay, @QueryParam("lPara") String lPara, @QueryParam("lBCK01") Integer lBCK01,
        @QueryParam("beginTime") String beginTime, @QueryParam("endTime") String endTime,@QueryParam("vad01") int vad01,
        @QueryParam("lBCE01") Integer lBCE01, @QueryParam("BAS02") String BAS02, @QueryParam("aDate") String aDate, @QueryParam("whichDay") String whichDay)
            throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("lVAC51", lVAC51);
        hs.put("lPara", lPara);
        hs.put("lBCK01", lBCK01);
        hs.put("lBCE01", lBCE01);
        hs.put("BAS02", BAS02);
        hs.put("fenzhen", fenzhen);
        hs.put("aDate", aDate);
        Calendar calendarB = Calendar.getInstance(); 
        Calendar calendarA = Calendar.getInstance(); //得到日历
        Date date = new Date();
        Date dBefore = date;
        Date dAfter = date;
        calendarB.setTime(dBefore);
        calendarA.setTime(dAfter);
        switch (whichDay)
        {
            case "today":
                calendarB.add(Calendar.DAY_OF_MONTH, -1);  
                break;
            case "yesterday":
                calendarB.add(Calendar.DAY_OF_MONTH, -2);  
                calendarA.add(Calendar.DAY_OF_MONTH, -1);  
                break;
            case "thedaybeforeyesterday":
                calendarB.add(Calendar.DAY_OF_MONTH, -3);  
                calendarA.add(Calendar.DAY_OF_MONTH, -2);  
                break;
        }
        dBefore = calendarB.getTime();   
        dAfter = calendarA.getTime();   
        String date1 = new SimpleDateFormat("yyyy-MM-dd 23:45:00").format(dBefore);
        String date2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59").format(dAfter);
//        if(beginTime==null)
//        {
            beginTime = date1;
//        }
//        if(endTime==null)
//        {
            endTime = date2;
//        }
        hs.put("beginTime", beginTime);
        hs.put("endTime", endTime);
        hs.put("searchcondition", searchCondition);
        hs.put("searchvalue", searchValue);
        hs.put("vad01", vad01);
        return patientSearch.querywaitingandtransfertreatment_one(hs);
        
    }
    
    /* Description(医师工作站中检索候诊或转诊病人基本信息) */
    @SuppressWarnings("unchecked")
    @GET
    @Path("queryVac34")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo queryVac34(@QueryParam("vac01") long vac01)
            throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("vac01", vac01);
        return patientSearch.queryVac34(hs);
        
    }
    
    /**
     * 650 门诊医师工作站检索就诊或已诊病人信息
     * 
     * @param lVAC34 执行状态 0 候诊 1 接诊 2 完诊
     * @param lBCK01 执行科室ID
     * @param lDt1 起始时间
     * @param lDt2 结束时间
     * @param lPara
     * @param lBCE01 医生ID
     * @param VAC01 就诊ID
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @GET
    @Path("queryvisitdoctor")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo queryVisitDoctor(@QueryParam("searchcondition") String searchCondition,
        @QueryParam("searchvalue") String searchValue, @QueryParam("lVAC34") Integer lVAC34,
        @QueryParam("lBCK01") Integer lBCK01, @QueryParam("lDt1") String lDt1, @QueryParam("lDt2") String lDt2,
        @QueryParam("lPara") String lPara, @QueryParam("lBCE01") Integer lBCE01, @QueryParam("VAC01") Integer VAC01)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("lVAC34", lVAC34);
        hs.put("lBCK01", lBCK01);
        hs.put("lDt1", lDt1);
        hs.put("lDt2", lDt2);
        hs.put("lPara", lPara);
        hs.put("lBCE01", lBCE01);
        hs.put("VAC01", VAC01);
        hs.put("searchcondition", searchCondition);
        hs.put("searchvalue", searchValue);
        return patientSearch.queryVisitDoctor(hs);
        
    }
    
    
    /**
     * 门诊医生站查询所有病人信息
     * 
     * @param lVAC34 执行状态 0 候诊 1 接诊 2 完诊
     * @param lBCK01 执行科室ID
     * @param lDt1 起始时间
     * @param lDt2 结束时间
     * @param lPara
     * @param lBCE01 医生ID
     * @param VAC01 就诊ID
     * @param currPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @GET
    @Path("queryAllJiezhen")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo queryAllJiezhen(@QueryParam("searchcondition") String searchCondition,
        @QueryParam("searchvalue") String searchValue, @QueryParam("lVAC34") Integer lVAC34,
        @QueryParam("lBCK01") Integer lBCK01, @QueryParam("lDt1") String lDt1, @QueryParam("lDt2") String lDt2,
        @QueryParam("lPara") String lPara, @QueryParam("lBCE01") Integer lBCE01, @QueryParam("VAC01") Integer VAC01)
            throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("lVAC34", lVAC34);
        hs.put("lBCK01", lBCK01);
        hs.put("lDt1", lDt1);
        hs.put("lDt2", lDt2);
        hs.put("lPara", lPara);
        hs.put("lBCE01", lBCE01);
        hs.put("VAC01", VAC01);
        hs.put("searchcondition", searchCondition);
        hs.put("searchvalue", searchValue);
        return patientSearch.queryAllJiezhen(hs);
        
    }
    
    /**
     * 743 医嘱编辑窗口检索门诊病人信息
     * 
     * @param VAA01 病人ID
     * @param VAC01 就诊ID
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("doctoradvicepatient")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo doctorAdvicePatient(@FormParam("VAA01") Integer VAA01, @FormParam("VAC01") Integer VAC01,
        @FormParam("currPage") int currPage, @FormParam("pageSize") int pageSize)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("VAA01", VAA01);
        hs.put("VAC01", VAC01);
        return patientSearch.doctorAdvicePatient(hs, (currPage - 1) * pageSize, pageSize);
    }
    
    /**
     * 2124 门诊检索条件按拼音码检索病人
     * 
     * @param aday
     * @param lPara
     * @param BAS02 诊室
     * @param lBCE01 员工id
     * @param lBCK01
     * @param lDt1 末次开嘱时间
     * @param lDt2
     * @param lTxt
     * @param adate 日期
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("getpinyincodesearch")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo getPinYinCodeSearch(@FormParam("aday") int aday, @FormParam("spara") int spara,
        @FormParam("lPara") String lPara, @FormParam("BAS02") String BAS02, @FormParam("lBCE01") int lBCE01,
        @FormParam("lBCK01") int lBCK01, @FormParam("lDt1") String lDt1, @FormParam("lDt2") String lDt2,
        @FormParam("lTxt") String lTxt, @FormParam("adate") String adate, @FormParam("currPage") int currPage,
        @FormParam("pageSize") int pageSize)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("aday", aday);
        hs.put("lPara", lPara);
        hs.put("lDt1", lDt1 + " 00:00:00.000");
        hs.put("lDt2", lDt2 + " 23:59:59.999");
        hs.put("lPara", lPara);
        hs.put("lBCE01", lBCE01);
        hs.put("lBCK01", lBCK01);
        hs.put("spara", spara);
        hs.put("lTxt", lTxt);
        hs.put("adate", adate);
        return patientSearch.getPinYinCodeSearch(hs, (currPage - 1) * pageSize, pageSize);
    }
    
    /* ScriptId(871) */
    /* Description(门诊科室) */
    @GET
    @Path("finddepartments")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findDepartments(@QueryParam("bce01") int bce01,@QueryParam("bck01") int bck01)
        throws Exception
    {
        Map map = new HashMap();
        map.put("bce01", bce01);
        map.put("bck01", bck01);
        return patientSearch.findDepartments(map);
    }
    
    @GET
    @Path("finddepartments2")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findDepartments2(@QueryParam("bce01") int bce01,@QueryParam("bck01") int bck01)
        throws Exception
    {
        Map map = new HashMap();
        map.put("bce01", bce01);
        map.put("bck01", bck01);
        return patientSearch.findDepartments2(map);
    }
    
    /* Description(检索住院科室或病区) */
    @GET
    @Path("findDepartmentsZhuyuan")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findDepartmentsZhuyuan()
        throws Exception
    {
        return patientSearch.findDepartmentsZhuyuan();
    }
    
    /* Description(检索病人首页--病人历史诊断信息) */
    @GET
	@Path("findPatentVao1List")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo findPatentVao1List(@QueryParam("vaa01") long vaa01,
	                                @QueryParam("acf01") int acf01,
	                                @QueryParam("vaa07") long vaa07)throws Exception{
        HashMap map = new HashMap();
        map.put("vaa01", vaa01);
        map.put("vaa07", vaa07);
        map.put("acf01", acf01);
	    return patientSearch.findPatentVao1List(map);
	}
    
    /* ScriptId(784) */
    /* Description(检索就诊病人：单个病人信息) */
    /**
     * 
     * @param lVAC01 就诊id
     * @return 100 103010 29
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("getpatientinfo")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo getPatientInfo(@FormParam("lVAC01") long lVAC01, @FormParam("productid") int productid,
        @FormParam("programid") int programid, @FormParam("paramno") int paramno)
        throws Exception
    {
        HashMap map = new HashMap();
        map.put("lVAC01", lVAC01);
        map.put("productid", productid);
        map.put("programid", programid);
        map.put("paramno", paramno);
        return patientSearch.getPatientInfo(map);
    }
    
    /* ScriptId(7498) */
    /* Description(门诊病人预约复诊项目查询) */
    @SuppressWarnings("unchecked")
    @POST
    @Path("patientordersubsequentvisititemdemand")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo patientOrderSubsequentVisitItemDemand(@FormParam("VAA01") int VAA01,
        @FormParam("currPage") int currPage, @FormParam("pageSize") int pageSize)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("VAA01", VAA01);
        return patientSearch.patientOrderSubsequentVisitItemDemand(hs, (currPage - 1) * pageSize, pageSize);
    }
    
    /* ScriptId(7488) */
    /* Description(根据会员卡id,检索病人信息) */
    @SuppressWarnings("unchecked")
    @POST
    @Path("getpatientbyvipid")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo getPatientByVIPId(@FormParam("VAA01") int VAA01)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("VAA01", VAA01);
        return patientSearch.getPatientByVIPId(hs);
    }
    
    /* ScriptId(7487) */
    /* Description(门诊医师站病人列表，加载单个候诊病人) */
    /**
     * 
     * @param lVAC01
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("findwaitdoctorpatient")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findWaitDoctorPatient(@FormParam("lVAC01") int lVAC01)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("lVAC01", lVAC01);
        return patientSearch.findWaitDoctorPatient(hs);
    }
    
    /* ScriptId(7486) */
    /* Description(门诊医师站病人列表，加载单个就诊病人) */
    @SuppressWarnings("unchecked")
    @POST
    @Path("findseedoctorpatient")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findSeeDoctorPatient(@FormParam("lVAC01") int lVAC01)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("lVAC01", lVAC01);
        return patientSearch.findSeeDoctorPatient(hs);
    }
    /* ScriptId(2125) */
    
    /* Description(简易挂号检索病人信息) */
    /**
     * 
     * @param FieldName
     * @param context
     * @param field
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("simpleregistersearchpatient")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo simpleRegisterSearchPatient(@FormParam("FieldName") String FieldName,
        @FormParam("context") String context)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("FieldName", FieldName);
        hs.put("context", context);
        return patientSearch.simpleRegisterSearchPatient(hs);
    }
    /* ScriptId(2022) */
    
    /* Description(医技单病人信息) */
    @SuppressWarnings("unchecked")
    @POST
    @Path("medicalsearchpatient")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo medicalSearchPatient(@FormParam("CBM01") int CBM01)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("CBM01", CBM01);
        return patientSearch.medicalSearchPatient(hs);
    }
    
    /* ScriptId(788) */
    /* Description(单病人就诊记录) */
    /**
     * 
     * @param VAA01 病人id
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("seedoctorhistory")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo seeDoctorHistory(@FormParam("VAA01") int VAA01)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("VAA01", VAA01);
        return patientSearch.seeDoctorHistory(hs);
    }
    
    /* ScriptId(7335) */
    /* Description(判断病人类别是否医保) */
    /**
     * 
     * @param BDP07 病人类别名称
     * @param BDP02 是否医保病人 1 是 0 否
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("ismedicalinsurance")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo isMedicalInsurance(@FormParam("BDP07") int BDP07, @FormParam("BDP02") String BDP02)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("BDP07", BDP07);
        hs.put("BDP02", BDP02);
        return patientSearch.isMedicalInsurance(hs);
    }
    
    /**
     * 门诊系统参数批量查询
     * @throws Exception
     */
    @GET
    @Path("findSysparams")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findSysparams(@QueryParam("userid") String userid,@QueryParam("bck01") String bck01)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("userid", userid);
        hs.put("bck01", bck01);
        return patientSearch.findSysparams(hs);
    }
    
    /**
     * 门诊系统主题查询
     * @throws Exception
     */
    @GET
    @Path("findSysparams1")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findSysparams1(@QueryParam("userid") String userid)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("userid", userid);
        return patientSearch.findSysparams1(hs);
    }
    
    /**
     * 门诊系统参数批量查询
     * @throws Exception
     */
    @GET
    @Path("findYaofang")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findYaofang(@QueryParam("bau01") String bau01,@QueryParam("corporationid") int corporationid)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("bau01", bau01);
        hs.put("corporationid", corporationid);
        return patientSearch.findYaofang(hs);
    }
    
    /**
     * 门诊系统参数批量查询
     * @throws Exception
     */
    @GET
    @Path("findYaofang1")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo findYaofang1(@QueryParam("bau01") String bau01,@QueryParam("corporationid") int corporationid)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("bau01", bau01);
        hs.put("corporationid", corporationid);
        return patientSearch.findYaofang1(hs);
    }
    
    /**
     * 门诊系统参数批量修改
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @POST
    @Path("updateAllSysparams")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo updateAllSysparams(@FormParam("resultArr105002_0") String resultArr105002_0,
                                      @FormParam("resultArr105002_1") String resultArr105002_1,
                                      @FormParam("resultArr9999_1") String resultArr9999_1,
                                      @FormParam("resultArr9999_0") String resultArr9999_0,
                                      @FormParam("resultArrValue") String resultArrValue,
                                      @FormParam("userid") String userid,
                                      @FormParam("bck01") String bck01,
                                      @FormParam("allArr") String allArr)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("resultArr105002_0", resultArr105002_0);
        hs.put("resultArr105002_1", resultArr105002_1);
        hs.put("resultArr9999_1", resultArr9999_1);
        hs.put("resultArr9999_0", resultArr9999_0);
        hs.put("resultArrValue", resultArrValue);
        hs.put("userid", userid);
        hs.put("bck01", bck01);
        hs.put("allArr", allArr);
        return patientSearch.updateAllSysparams(hs);
    }
    
    /**
     * 门诊系统参数还原
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    @POST
    @Path("updateUserParams_cancel")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo updateUserParams_cancel(
        @FormParam("cancelArr_public") String cancelArr_public,
        @FormParam("cancelArr_user") String cancelArr_user,
        @FormParam("userid") String userid)
            throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("cancelArr_public", cancelArr_public);
        hs.put("cancelArr_user", cancelArr_user);
        hs.put("userid", userid);
        return patientSearch.updateUserParams_cancel(hs);
    }
    
    /**
     * 根据就诊ID查询费用信息
     * 
     * @throws Exception
     */
    @GET
    @Path("queryVaj1ByVac01")
    @Produces({MediaType.APPLICATION_JSON})
    public ModelVo queryVaj1ByVac01(@QueryParam("params") String params)
        throws Exception
    {
        HashMap hs = (HashMap)JsonUtil.getMap(params);
        return patientSearch.queryVaj1ByVac01(hs);
    }
    
}
